<template>
    <div class="regins">
        <div class="register">

            <el-form ref="form" :model="form" label-width="10vw">
                <el-form-item label="用户名">
                    <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
                </el-form-item>
                <el-form-item label="密码">
                    <el-input v-model.trim="form.pass" placeholder="请输入密码" show-password></el-input>
                </el-form-item>
                <el-form-item label="确认密码">
                    <el-input v-model.trim="form.checkPass" placeholder="请确认密码" show-password></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" :plain="true" @click="onRegister">注册</el-button>
                    <el-button type="primary" @click="onReset">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
import {Message} from "element-ui"
import {req_register} from "@/api/loginRegister.js"
export default {
    data() {
        return {
            form: {
                name: "",
                pass: "",
                checkPass: "",
            }
        }
    },
    methods: {
        // 点击注册按钮时，发送Ajax请求，将数据提交到服务器，再由服务器提交到数据库
        async onRegister() {
            if (this.form.pass === this.form.checkPass) {
                console.log("发生ajax请求");
                let result = await req_register({
                    method: "post",
                    url: '/register',
                    arg: {
                        user_name: this.form.name,
                        password: this.form.pass,
                    },
                    headerType: "form",
                });
                console.log(result);
                // 当注册成功后，跳转到登录页面
                if(result.data == "注册成功！！"){
                    this.$router.push({
                        path:'/login'
                    })
                }
            } else {
                console.log("密码不一致，请重新输入");
                // 当密码不一致时，弹出警告框，注意：要引入该组件
                Message({
                    type:'warning',
                    message:'密码错误，不一致，请重新输入'
                })
            }
            // 当注册后清空密码框
            this.form.pass = "";
            this.form.checkPass
        },
        // 重置表单内一切input框的信息
        onReset() {
            this.form.name="";
            this.form.pass="";
            this.form.checkPass="";
        }
    }
}
</script>

<style lang="scss" scoped>
.regins {
    background: url("../../assets/images/注册.webp") no-repeat center center;
    background-size: 100% 100%;
    display: flex;
    justify-content: center;
    align-items: center;

    .register {
        width: 400px;
        height: 240px;
        border-radius: 20px;
        box-shadow: 0 0 10px 10px #95C1D9;
        background: url("../../assets/images/注册.webp") no-repeat center center;
        background-size:100% 100%;
       
        .el-input{
            width:300px ;

        }
        .el-form-item{
            height: 40px;
        }
    }
}
</style>